package com.living.player.services;

import android.annotation.TargetApi;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import com.living.player.widget.media.FileMediaDataSource;

import java.io.File;
import java.io.IOException;

import tv.danmaku.ijk.media.exo.IjkExoMediaPlayer;
import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.misc.IMediaDataSource;

public class ServicePlayUtils {

    //    @TargetApi(Build.VERSION_CODES.M)
    protected static void playPath(Context mAppContext, String path) {
        stopPlayback(mAppContext);
        Uri mUri;
        if (path.contains("adaptationSet")) {
            mUri = Uri.EMPTY;
        } else {
            mUri = Uri.parse(path);
        }
        if (mUri == null) {
            // not ready for playback just yet, will try again later
            return;
        }
        AudioManager am = (AudioManager) mAppContext.getSystemService(Context.AUDIO_SERVICE);
        am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
        IMediaPlayer mMediaPlayer = MediaPlayerService.getMediaPlayer();
        try {
            if (mMediaPlayer != null) {
                mMediaPlayer.stop();
                mMediaPlayer.release();
            }
            mMediaPlayer = createPlayer(mAppContext);
            MediaPlayerService.setMediaPlayer(mMediaPlayer);
            // TODO: create SubtitleController in MediaPlayer, but we need
            String scheme = mUri.getScheme();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && (TextUtils.isEmpty(scheme) || scheme.equalsIgnoreCase("file"))) {
                IMediaDataSource dataSource = new FileMediaDataSource(new File(mUri.toString()));
                mMediaPlayer.setDataSource(dataSource);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                mMediaPlayer.setDataSource(mAppContext, mUri, null);
            } else {
                mMediaPlayer.setDataSource(mUri.toString());
            }
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.setScreenOnWhilePlaying(true);
            mMediaPlayer.prepareAsync();
        } catch (IOException ex) {
//            Log.w(TAG, "Unable to open content: " + mUri, ex);
//            mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        } catch (IllegalArgumentException ex) {
//            Log.w(TAG, "Unable to open content: " + mUri, ex);
//            mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        } finally {
            // REMOVED: mPendingSubtitleTracks.clear();
        }
        mMediaPlayer.start();
    }

    public static void stopPlayback(Context mContext) {
        IMediaPlayer sMediaPlayer = MediaPlayerService.getMediaPlayer();
        if (sMediaPlayer != null) {
            sMediaPlayer.stop();
            sMediaPlayer.release();
            sMediaPlayer = null;
            AudioManager am = (AudioManager) mContext.getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
            am.abandonAudioFocus(null);
        }
    }

    public static IMediaPlayer createPlayer(Context mContext) {
        IMediaPlayer mediaPlayer = null;
        IjkExoMediaPlayer IjkExoMediaPlayer = new IjkExoMediaPlayer(mContext.getApplicationContext());
        mediaPlayer = IjkExoMediaPlayer;
        return mediaPlayer;
    }


}
